//
//  UINavigationController+Extension.swift
//  SK_BasePrivateProject
//
//  Created by mac on 2018/10/08.
//  Copyright © 2018年 Regina. All rights reserved.
//

import Foundation
import UIKit

var navigationBottomLineKey = "navigationBottomLine"

extension UINavigationController {
    
    private var navigationBottomLine: UIImageView? {
        get {
            return objc_getAssociatedObject(self, navigationBottomLineKey) as? UIImageView
        }
        set {
            objc_setAssociatedObject(self, navigationBottomLineKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
    
    public func setup() {
        self.setupNavigationBarTheme()
//        self.setupNavigationBarBottomLine()
    }
    
    func setupNavigationBarTheme() {
        let appearance: UINavigationBar = UINavigationBar.appearance()
        
        appearance.isTranslucent = true
        appearance.barStyle = .default
        appearance.tintColor = .white
//        let rgb:CGFloat = 0.1
//        appearance.barTintColor = UIColor.init(red: rgb, green: rgb, blue: rgb, alpha: 0.65)
        appearance.barTintColor = UIColor.white
        
        let shadow = NSShadow.init()
        shadow.shadowOffset = CGSize.zero
        appearance.titleTextAttributes = [NSAttributedStringKey.font:REFontRegular(18.0),
                                          NSAttributedStringKey.foregroundColor:UIColor.black,
                                          NSAttributedStringKey.shadow:shadow]
        appearance.shadowImage = UIImage()
        
        
        let size: CGSize = CGSize(width: REScreenWidth, height: UIApplication.shared.statusBarFrame.size.height + appearance.frame.size.height)
        let backgroundImage = UIImage.init(color: .white, size: size)
        appearance.setBackgroundImage(backgroundImage, for: UIBarMetrics.default)
        
        appearance.isTranslucent = false
    
    }
        
    func findHairlineImageViewUnder(view: UIView) -> UIImageView? {
        if view.isKind(of: UIImageView.classForCoder()) && view.bounds.size.height <= 1.0 {
            return view as? UIImageView
        }
        for subview: UIView in view.subviews {
            if let imageView = self.findHairlineImageViewUnder(view: subview) {
                return imageView
            }
        }
        return nil
    }
    
    func setupNavigationBarBottomLine() {
        guard let navigationBarBottomLine = self.findHairlineImageViewUnder(view: self.navigationBar) else {
            return
        }
        navigationBarBottomLine.isHidden = true
        
        self.navigationBottomLine = navigationBarBottomLine
    }
    
    public func showNavigationBottomLine() {
        self.navigationBottomLine?.isHidden = false
    }
    
    public func hideNavigationBottomLine() {
        self.navigationBottomLine?.isHidden = true
        
    }
}
